package com.ruoyi.fsepc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GDMapUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.fsepc.domain.StFseLocation;
import com.ruoyi.fsepc.domain.StFseLocationLog;
import com.ruoyi.fsepc.service.IStFseLocationLogService;
import com.ruoyi.fsepc.service.IStFseLocationService;
import com.ruoyi.system.api.domain.constant.ConfigKeys;
import com.ruoyi.system.api.service.IConfigurationService;
import com.ruoyi.system.api.service.LocationService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * FSE位置坐标日志Controller
 *
 * @author ruoyi
 * @date 2020-02-27
 */
@Controller
@RequestMapping("/fse/locationlog")
public class StFseLocationLogController extends BaseController {
    private String prefix = "fsepc/locationlog";

    @Autowired
    private IStFseLocationLogService stFseLocationLogService;

    @Autowired
    private IStFseLocationService stFseLocationService;
    @Autowired
    private IConfigurationService configurationService;
    @Autowired
    private LocationService locationService;

    /**
     * 历史监控
     *
     * @return
     */
    @GetMapping("/history")
    public String history(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        Date date = new Date();
        String end = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        String start = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", yesterday);
        mmap.put("roleId", roleId);
        mmap.put("tlId", StringUtils.isEmpty(user.getTeamleaderId()) ? "" : user.getTeamleaderId());
        mmap.put("start", start);
        mmap.put("end", end);
        return prefix + "/history";
    }
    /**
     * 查询team下fse历史位置信息
     *
     * @param fseLocationLog
     * @return
     */
//    @PostMapping("/getFseLocationLog")
//    @ResponseBody
//    public AjaxResult getFseLocationLog(StFseLocationLog fseLocationLog) {
//        SysUser user = ShiroUtils.getSysUser();
//        Long roleId = user.getRoles().get(0).getRoleId();
//        List<StFseLocationLog> list = null;
//
//        //经理 roleID=5； 经理登录查询所有FSE
//
//        if (5 == roleId || 4 == roleId) {
//            list = stFseLocationLogService.deleteStFseLocationLogById().getFseLocation(fseLocationLog);
//        } else {
//            fseLocation.setEnginnerId(user.getEnginnerId().toString());
//            fseLocation.setTeamleaderId(user.getEnginnerId().toString());
//            list = stFseLocationService.getStFseLocation(fseLocation);
//        }
//
//        return AjaxResult.success("查询成功！", list);getFseReportNoticeFourTypeAll
//    }

    /**
     * 根据车辆SIM卡号查询车辆状态历史列表
     */
    @PostMapping("/selectHistoryList")
    @ResponseBody
    public AjaxResult selectHistoryList(StFseLocationLog stFseLocationLog) {
//        if (!DateUtils.isWorkTime() || !DateUtils.isDatePeriod()) {
//            return AjaxResult.success("不在工作时间！", new ArrayList<>());
//        }
        List<StFseLocationLog> list = stFseLocationLogService.selectHistoryList(stFseLocationLog);
        if(!ObjectUtils.isEmpty(list)){
            list = stFseLocationLogService.dealWithFseLocationLog(list);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return AjaxResult.success("查询成功！", map);
    }

    /**
     * 根据车辆SIM卡号查询车辆状态历史列表
     */
    @PostMapping("/listPage")
    @ResponseBody
    public TableDataInfo listPage(StFseLocationLog stFseLocationLog) {
        List<StFseLocationLog> list = new ArrayList<>();
        if (stFseLocationLog.getFseId() != null) {
            startPage();
            list = stFseLocationLogService.selectHistoryList(stFseLocationLog);
            for (StFseLocationLog info : list) {
                info.setAddress(GDMapUtils.getAddressByCoordinate(info.getPostx(), info.getPosty()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 跳转日志列表
     *
     * @return
     */
    @RequiresPermissions("fse:locationlog:view")
    @GetMapping()
    public String log() {
        return prefix + "/log";
    }

    /**
     * 查询FSE位置坐标日志列表
     */
    @RequiresPermissions("fse:locationlog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StFseLocationLog stFseLocationLog) {
        startPage();
        List<StFseLocationLog> list = stFseLocationLogService.selectStFseLocationLogList(stFseLocationLog);
        return getDataTable(list);
    }

    /**
     * 导出FSE位置坐标日志列表
     */
    @RequiresPermissions("fse:locationlog:export")
    @Log(title = "FSE位置坐标日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StFseLocationLog stFseLocationLog) {
        List<StFseLocationLog> list = stFseLocationLogService.selectStFseLocationLogList(stFseLocationLog);
        ExcelUtil<StFseLocationLog> util = new ExcelUtil<StFseLocationLog>(StFseLocationLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 导出fse历史信息
     *
     * @author qinheyang
     * @date 2020-04-30
     */
    @Log(title = "FSE位置坐标日志", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByRole")
    @ResponseBody
    public AjaxResult exportByRole(StFseLocationLog stFseLocationLog, StFseLocation stFseLocation) {
        List<StFseLocationLog> list = new ArrayList<>();
            /*当用户想导出某一个人某一段时间的时候*/
            if (stFseLocationLog.getStartTime() != null && stFseLocationLog.getEndTime() != null) {
                if (stFseLocationLog.getFseId() != null && !stFseLocationLog.getFseId().equals("")) {
                    list = stFseLocationLogService.selectHistoryList(stFseLocationLog);
                }
            }
        /* 当用户只想导出某一个人的历史记录的时候*/

            if (stFseLocationLog.getStartTime() == null || stFseLocationLog.getEndTime() == null) {
                list = stFseLocationLogService.newSelectHistoryList(stFseLocationLog);
                /*当用户想导出全部的时候*/
                if (stFseLocationLog.getFseId() == null || stFseLocationLog.getFseId().equals("")) {
                    SysUser user = ShiroUtils.getSysUser();
                    Long roleId = user.getRoles().get(0).getRoleId();
                    List<StFseLocation> list2 = stFseLocationService.getFseLocation(stFseLocation);
                    List<StFseLocation> list1 = new ArrayList<>();
                    for (StFseLocation stFseLocation1 : list2) {
                        if ((stFseLocation1.getPostx() == null || stFseLocation1.getPosty() == null) && roleId != 3) {
                            continue;
                        }
                        list1.add(stFseLocation1);
                    }
                    list.clear();
                    for (StFseLocation stFseLocation2 : list1) {
                        stFseLocationLog.setFseId(stFseLocation2.getFseId());
                        List<StFseLocationLog> list3 = stFseLocationLogService.newSelectHistoryList(stFseLocationLog);
                        if (list3.size() != 0) {
                            for (StFseLocationLog stFseLocationLog1 : list3) {
                                list.add(stFseLocationLog1);
                            }
                        }
                    }
                }
            }

            for (StFseLocationLog stFseLocationLog3 : list) {
                if (stFseLocationLog3.getState() != null) {
                    if (stFseLocationLog3.getState().equals("1")) {
                        stFseLocationLog3.setState("可用");
                    } else if (stFseLocationLog3.getState().equals("0")) {
                        stFseLocationLog3.setState("不可用");
                    }
                }

            }

        if(!ObjectUtils.isEmpty(list)){
            try {
                list =   stFseLocationLogService.dealWithFseLocationLog(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (StFseLocationLog stFseLocationLog3 : list) {
            if(StringUtils.equals("1",stFseLocationLog3.getType())){
                stFseLocationLog3.setType("定位上传");
            }
            if(StringUtils.equals("2",stFseLocationLog3.getType())){
                stFseLocationLog3.setType("签到上传");
            }
            if(StringUtils.equals("3",stFseLocationLog3.getType())){
                stFseLocationLog3.setType("签离上传");
            }
            if(StringUtils.equals("4",stFseLocationLog3.getType())){
                stFseLocationLog3.setType("自动签离");
            }
        }
        ExcelUtil<StFseLocationLog> util = new ExcelUtil<StFseLocationLog>(StFseLocationLog.class);
        return util.exportExcel(list, "log");
    }

    /**
     * 新增FSE位置坐标日志
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存FSE位置坐标日志
     */
    @RequiresPermissions("fse:locationlog:add")
    @Log(title = "FSE位置坐标日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StFseLocationLog stFseLocationLog) {
        return toAjax(stFseLocationLogService.insertStFseLocationLog(stFseLocationLog));
    }

    /**
     * 修改FSE位置坐标日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        StFseLocationLog stFseLocationLog = stFseLocationLogService.selectStFseLocationLogById(id);
        mmap.put("stFseLocationLog", stFseLocationLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存FSE位置坐标日志
     */
    @RequiresPermissions("fse:locationlog:edit")
    @Log(title = "FSE位置坐标日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StFseLocationLog stFseLocationLog) {
        return toAjax(stFseLocationLogService.updateStFseLocationLog(stFseLocationLog));
    }

    /**
     * 删除FSE位置坐标日志
     */
    @RequiresPermissions("fse:locationlog:remove")
    @Log(title = "FSE位置坐标日志", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stFseLocationLogService.deleteStFseLocationLogByIds(ids));
    }
}
